﻿namespace AzManAPI.AzManClient.ViewModel
{
    using System;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using AzManAPI.AzManClient.Helpers;

    public class PolicyStoreViewModel : INotifyPropertyChanged
    {
        private PolicyStore policyStore;

        public bool IsConnected
        {
            get { return policyStore.IsConnected; }
        }

        public bool CanModifyPolicyUrl
        {
            get { return !policyStore.IsConnected; }
        }

        public string PolicyUrl
        {
            get { return policyStore.PolicyUrl; }
            set { policyStore.PolicyUrl = value; }
        }

        public ObservableCollection<Application> Applications
        {
            get { return new ObservableCollection<Application>(policyStore.Applications); }
        }

        public PolicyStoreViewModel(PolicyStore policyStore)
        {
            this.policyStore = policyStore;

            this.policyStore.PropertyChanged += delegate(object sender, PropertyChangedEventArgs e)
            {
                OnPropertyChanged(e);
            };
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, e);
            }

            if (e.PropertyName == "IsConnected")
            {
                OnPropertyChanged(new PropertyChangedEventArgs("CanModifyPolicyUrl"));
            }
        }

        #endregion
    }
}